package org.baicaix.demo.gravity {
	
	import org.baicaix.events.DataEvent;
	import org.baicaix.utils.ObjectPoolManager;
	import org.baicaix.utils.TextFieldUtils;

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.Dictionary;

	/**
	 * @author dengyang
	 * @date 2010-4-22
	 */
	public class GravityTest extends Sprite {
		
		private var starCount : TextField;

		public function GravityTest() {
			initStage();
			initEvent();
			
			starCount = this.addChild(new TextField()) as TextField;
			starCount.text = "0";
			
			stars = new Dictionary();
			ObjectPoolManager.getInstance().init(stage);	
		}
		
		private function initStage() : void {
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
		}

		private function initEvent() : void {
			stage.addEventListener(MouseEvent.CLICK, clickHandler);
			this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			ObjectPoolManager.getInstance().addEventListener(Star + "countChange", starCountChangeHandler);
		}
		
		private var stars : Dictionary;
		private function clickHandler(event : MouseEvent) : void {
			var star : Star = ObjectPoolManager.getInstance().get(Star) as Star;
			star.x = event.stageX;
			star.y = event.stageY;
			stars[star] = star;
		}

		private function enterFrameHandler(event : Event) : void {
			for each (var star : Star in stars) {
				star.y += 3;
				if(star.y > stage.fullScreenHeight) {
					delete stars[star];
					ObjectPoolManager.getInstance().recycle(Star, star);
				}
			}
		}

		private function starCountChangeHandler(event : DataEvent) : void {
			starCount.text = (event.data as int) + "";
		}
	}
}
